package com.noble.glamour.ui.discover;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.noble.glamour.R;
import com.noble.glamour.bean.center.user.light.UserInfoLightweight;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.baseui.ExBaseAdapter;
import com.noble.glamour.module.logic.config.Constant;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.glamour.module.util.UIShow;
import com.noble.glamour.ui.agora.model.AgoraConstant;
import com.noble.glamour.ui.user.paygoods.GoodsConstant;
import com.noble.library.image.ImageLoader;

import java.util.List;

/**
 * 语聊
 * Created by Su on 2017/8/9.
 */

public class ChatFragmentAdapter extends ExBaseAdapter<UserInfoLightweight> {
    private static final int RTC_CHAT_VIDEO = 1;    // 视频通信
    private static final int RTC_CHAT_VOICE = 2;    // 音频通信

    public ChatFragmentAdapter(Context context, List<UserInfoLightweight> datas) {
        super(context, datas);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final MyViewHolder holder;
        if (convertView == null) {
            convertView = inflate(R.layout.f1_chat_fragment_item);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }

        final UserInfoLightweight userInfo = getItem(position);
        ImageLoader.loadRoundAvatar(getContext(), userInfo.getAvatar(), holder.iv_avatar);
        holder.tv_name.setText(userInfo.getNickname());
        holder.iv_vip.setVisibility(userInfo.isVip() ? View.VISIBLE : View.GONE);
        holder.tv_price.setText(getContext().getString(R.string.user_other_set_chat_price, userInfo.getPrice()));

        if (userInfo.isToper()) {
            holder.iv_ranking.setVisibility(View.VISIBLE);
            if (userInfo.isMan()) {
                holder.iv_ranking.setImageResource(R.drawable.f1_top02);
            } else {
                holder.iv_ranking.setImageResource(R.drawable.f1_top01);
            }
        } else {
            holder.iv_ranking.setVisibility(View.GONE);
        }

        if (userInfo.getAge() == 0) {
            holder.tv_age.setVisibility(View.GONE);
        } else {
            holder.tv_age.setVisibility(View.VISIBLE);
            holder.tv_age.setText(userInfo.getAge() + "岁");
        }

        if (userInfo.getHeight() == 0) {
            holder.tv_height.setVisibility(View.GONE);
        } else {
            holder.tv_height.setVisibility(View.VISIBLE);
            holder.tv_height.setText(userInfo.getHeight() + "cm");
        }

        if (TextUtils.isEmpty(userInfo.getDistance())) {
            holder.tv_distance.setVisibility(View.GONE);
        } else {
            holder.tv_distance.setVisibility(View.VISIBLE);
            holder.tv_distance.setText(userInfo.getDistance());
        }

        if ((userInfo.getAge() != 0 || userInfo.getHeight() != 0) && !TextUtils.isEmpty(userInfo.getDistance())) {
            holder.point.setVisibility(View.VISIBLE);
        } else {
            holder.point.setVisibility(View.GONE);
        }

        final boolean isCancel = ModuleMgr.getRtcChatMgr().inviteCancel(userInfo.getInvite_id());
        if (userInfo.getVtype() == RTC_CHAT_VIDEO) {
            holder.call_back.setBackgroundResource(isCancel ? R.drawable.f1_chat_video_callback : R.drawable.ic_video_primary_btn_video);
//            holder.call_state.setText(isCancel ? getContext().getString(R.string.chat_invite_canceled, getContext().getString(R.string.video)) :
//                    getContext().getString(R.string.chat_invite, getContext().getString(R.string.video)));
        } else {
            holder.call_back.setBackgroundResource(isCancel ? R.drawable.f1_chat_voice_callback : R.drawable.ic_video_primary_btn_audio);
//            holder.call_state.setText(isCancel ? getContext().getString(R.string.chat_invite_canceled, getContext().getString(R.string.audio)) :
//                    getContext().getString(R.string.chat_invite, getContext().getString(R.string.audio)));
        }

        holder.rel_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ModuleMgr.getCommonMgr().getCommonConfig().getVideo().isVideoCallNeedVip() // 需要vip
                        && !ModuleMgr.getCenterMgr().getMyInfo().isVip()) {
                    UIShow.showGoodsVipBottomDlg(getContext());
                } else {
                    UIShow.showCheckOtherInfoAct(getContext(), userInfo.getUid());
                }
            }
        });

        holder.iv_avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ModuleMgr.getCommonMgr().getCommonConfig().getVideo().isVideoCallNeedVip() // 需要vip
                        && !ModuleMgr.getCenterMgr().getMyInfo().isVip()) {
                    UIShow.showGoodsVipBottomDlg(getContext());
                } else {
                    UIShow.showCheckOtherInfoAct(getContext(), userInfo.getUid());
                }
            }
        });

        holder.call_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ModuleMgr.getRtcChatMgr().chatFragmentInvite((Activity) getContext(), userInfo.getUid(),
                        isCancel || !holder.inviteSuc ? 0 : userInfo.getInvite_id(), userInfo.getVtype(), userInfo.getPrice(), new RequestComplete() {
                            @Override
                            public void onRequestComplete(HttpResponse response) {
                                if (response.isOk()) return;

                                holder.inviteSuc = false;
                                userInfo.setInvite_id(0);
                                if (userInfo.getVtype() == RTC_CHAT_VIDEO) {
                                    holder.call_back.setBackgroundResource(R.drawable.f1_chat_video_callback);
                                    ModuleMgr.getRtcChatMgr().inviteVideo((Activity) getContext(), userInfo.getUid(), AgoraConstant.APPEAR_TYPE_NO,"");
                                } else {
                                    holder.call_back.setBackgroundResource(R.drawable.f1_chat_voice_callback);
                                    ModuleMgr.getRtcChatMgr().inviteVoice((Activity) getContext(), userInfo.getUid(),"");
                                }
                            }
                        });
            }
        });
        return convertView;
    }

    class MyViewHolder {
        private ImageView iv_avatar, iv_vip, iv_ranking;
        private TextView tv_name, tv_age, tv_height, tv_distance, tv_price;
        private Button call_back;
        private TextView call_state;
        private RelativeLayout rel_item;
        private View point;
        private boolean inviteSuc = true;

        public MyViewHolder(View convertView) {
            initView(convertView);
        }

        private void initView(View convertView) {
            iv_avatar = (ImageView) convertView.findViewById(R.id.discover_item_avatar);
            iv_vip = (ImageView) convertView.findViewById(R.id.discover_item_vip_state);

            call_state = (TextView) convertView.findViewById(R.id.calling_state);

            tv_name = (TextView) convertView.findViewById(R.id.discover_item_name);
            tv_age = (TextView) convertView.findViewById(R.id.discover_item_age);
            tv_height = (TextView) convertView.findViewById(R.id.discover_item_height);
            tv_distance = (TextView) convertView.findViewById(R.id.discover_item_distance);

            call_back = (Button) convertView.findViewById(R.id.chat_call_back);

            iv_ranking = (ImageView) convertView.findViewById(R.id.discover_item_ranking_state);
            rel_item = (RelativeLayout) convertView.findViewById(R.id.discover_item);

            point = convertView.findViewById(R.id.discover_item_point);
            tv_price = (TextView) convertView.findViewById(R.id.chat_price);
        }
    }
}
